package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.BookConsultInfoMapper;
import com.cdks.wuxiu.entity.mysql.BookConsultInfo;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 预约咨询 服务实现类
 */
@Service
public class BookConsultInfoServiceImpl extends ServiceImpl<BookConsultInfoMapper, BookConsultInfo> implements BookConsultInfoService {
    @Override
    public int createSheet(BookConsultInfo order) {
        baseMapper.insert(order);
        
        return 0;
    }

    @Override
    public BookConsultInfo getSheetByOrderNo(String orderNo) {
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BookConsultInfo::getOrderNo, orderNo);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public BookConsultInfo getBusySheetByUser(long userId, long time) {
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_STOP)
                        .le(BookConsultInfo::getStartTime, time)
                        .ge(BookConsultInfo::getExpireTime, time);        
        wrapper.last("AND ((payer=" + userId + ") OR (earner=" + userId + "))");

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public BookConsultInfo getBusySheetAnyUser(long userId, long recverId, long time) {
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_STOP)
                        .le(BookConsultInfo::getStartTime, time)
                        .ge(BookConsultInfo::getExpireTime, time);
        wrapper.last("AND ((payer IN(" + userId + "," + recverId + ")) OR (earner IN (" + userId + "," + recverId + ")))");

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public BookConsultInfo getBusySheetBothUser(long senderId, long recverId, long time) {
        List<Long> ids  = Arrays.asList(senderId, recverId);
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_STOP)
                        .le(BookConsultInfo::getStartTime, time)
                        .ge(BookConsultInfo::getExpireTime, time)
                        .in(BookConsultInfo::getPayer, ids)
                        .in(BookConsultInfo::getEarner, ids);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public BookConsultInfo getPlanSheetByUser(long userId, long startTime, long expireTime) {
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_STOP);

        String lastSql = ("AND ((startTime BETWEEN " + startTime + " AND " + expireTime + ") OR (expireTime BETWEEN " + startTime + " AND " + expireTime + "))");
        lastSql  += (" AND ((payer=" + userId + ") OR (earner=" + userId + "))");
        lastSql  += (" LIMIT 1");

        return baseMapper.selectOne(wrapper.last(lastSql));
    }

    @Override
    public BookConsultInfo getPlanSheetAnyUser(long userId, long recverId, long startTime, long expireTime) {
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_STOP);

        String lastSql = ("AND ((startTime BETWEEN " + startTime + " AND " + expireTime + ") OR (expireTime BETWEEN " + startTime + " AND " + expireTime + "))");
        lastSql  += (" AND ((payer IN(" + userId + "," + recverId + ")) OR (earner IN (" + userId + "," + recverId + ")))");
        lastSql  += (" LIMIT 1");

        return baseMapper.selectOne(wrapper.last(lastSql));
    }

    @Override
    public int paySheet(BookConsultInfo order) {
        UpdateWrapper<BookConsultInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(BookConsultInfo::getOrderNo, order.getOrderNo())
                        .eq(BookConsultInfo::getStatus, Constant.BOOK_STATUS_NOPAY);

        int ret  = baseMapper.update(order, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int confirmSheet(BookConsultInfo order) {
        UpdateWrapper<BookConsultInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(BookConsultInfo::getOrderNo, order.getOrderNo())
                        .eq(BookConsultInfo::getStatus, Constant.BOOK_STATUS_NOPAY);

        int ret  = baseMapper.update(order, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int signInSheet(BookConsultInfo order, int lastStatus) {
        UpdateWrapper<BookConsultInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(BookConsultInfo::getOrderNo, order.getOrderNo())
                        .eq(BookConsultInfo::getSignInStatus, lastStatus)
                        .eq(BookConsultInfo::getStatus, Constant.BOOK_STATUS_PAYED);

        int ret  = baseMapper.update(order, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int changeSheet(BookConsultInfo order) {
        UpdateWrapper<BookConsultInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(BookConsultInfo::getOrderNo, order.getOrderNo())
                        .lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_START);

        int ret  = baseMapper.update(order, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int finishSheet(BookConsultInfo order) {
        UpdateWrapper<BookConsultInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(BookConsultInfo::getOrderNo, order.getOrderNo())
                        .lt(BookConsultInfo::getStatus, Constant.BOOK_STATUS_STOP);

        int ret  = baseMapper.update(order, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public List<BookConsultInfo> getRecord(long userId, int page, int start, int stop) {
        QueryWrapper<BookConsultInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().between(BookConsultInfo::getCreateTime, start, stop);
        wrapper.last("AND ((payer=" + userId + ") OR (earner=" + userId + ")) ORDER BY id DESC LIMIT " + page * 10 + "," + 10);

        return baseMapper.selectList(wrapper);
    }
}
